package com.nate.tasking.actions;

import java.util.List;

import javax.jdo.PersistenceManager;

import org.apache.log4j.Logger;

import com.nate.tasking.data.PMF;
import com.nate.tasking.domain.Project;
import com.nate.tasking.domain.Task;
import com.opensymphony.xwork2.ActionSupport;


public class AddTaskAction extends ActionSupport {

	
	private static final long serialVersionUID = 6873040788453682095L;
	private final Logger logger = Logger.getLogger(AddTaskAction.class);
	private Task task;
	private List<Project> listOfProjects;
	
	public String execute(){
		logger.debug("AddTaskAction start");
		
		logger.debug("adding Task: " + task.getName());
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    	try {
	            pm.makePersistent(task);
	            logger.debug("Task Added");
	        } finally {
	            pm.close();
	        }
		
		return SUCCESS;
	}
	
	public String input(){
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
    	try {
    		String query = "select from " + Project.class.getName();
    	    listOfProjects = (List<Project>) pm.newQuery(query).execute();
    	    System.out.println("project list size: " + listOfProjects.size());
            logger.debug("retrieved projects");
        } finally {
            pm.close();
        }
		
		return INPUT;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public List<Project> getListOfProjects() {
		return listOfProjects;
	}

	public void setListOfProjects(List<Project> listOfProjects) {
		this.listOfProjects = listOfProjects;
	}
	
	
	
}
